package com.sun.local.top;

import cn.hutool.core.io.FileUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.sun.common.constants.Constant.SSQ_FILE_PATH;

/**
 * 历史最大奖金
 */
public class TopMaxBonus {

    public static void main(String[] args) {
        getTopMaxBonus();
    }

    private static void getTopMaxBonus() {
        // 读取文件数据
        List<String> lines = FileUtil.readUtf8Lines(SSQ_FILE_PATH);

        Map<String, String> bonusMap = new HashMap<>();
        Map<String, String> jackpotMap = new HashMap<>();
        for (String ssq : lines) {
            String[] line = ssq.split("\\|");
            // 日期
            String date = line[0];
            // 奖金
            String bonusLine = line[3];
            String replace = bonusLine.replace(",", "");
            String[] split = replace.split("\\*");
            BigDecimal b1 = new BigDecimal(split[0]);
            BigDecimal b2 = new BigDecimal(split[1]);

            bonusMap.put(date, b1.multiply(b2).toString());
            jackpotMap.put(date, line[4].replace(",", ""));
        }

        // 奖池
        Map<String, String> jackpotSortMap = jackpotMap.entrySet().stream()
                .sorted((e1, e2) -> Long.compare(Long.parseLong(e2.getValue()), Long.parseLong(e1.getValue())))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        for (Map.Entry<String, String> entry : jackpotSortMap.entrySet()) {
            System.out.println("历史最高奖池：" + entry.getKey() + "->" + convertSeparator(entry.getValue()));
            break;
        }

        // 排列
        Map<String, String> bonusSortMap = bonusMap.entrySet().stream()
                .sorted((e1, e2) -> Long.compare(Long.parseLong(e2.getValue()), Long.parseLong(e1.getValue())))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        // 获取输出内容
        List<String> outputList = buildOutputContent(bonusSortMap);
        System.out.println("历史最高奖金：" + outputList.get(0));
    }

    /**
     * 构建输出内容
     */
    private static List<String> buildOutputContent(Map<String, String> bonusMap) {
        List<String> outputList = new ArrayList<>();
        int count = 0;
        for (Map.Entry<String, String> entry : bonusMap.entrySet()) {
            if(count ++ > 10){
                break;
            }
            String date = entry.getKey();
            String bonus = convertSeparator(entry.getValue());
            outputList.add(date + "->" + bonus);
        }
        return outputList;
    }

    /**
     * 将千位分隔转换成万位分隔
     *
     * @param oneThousandStr 千位分隔字符串
     * @return 万位分隔字符串
     */
    public static String convertSeparator(String oneThousandStr) {
        String replace = oneThousandStr.replace(",", "");

        int length = replace.length();
        if(length <= 4) {
            return replace;
        }

        // 从后往前数，每4个字符添加一个逗号
        StringBuilder thousandStr = new StringBuilder();
        int i = length - 1;
        while (i >= 0) {
            thousandStr.insert(0, replace.charAt(i));
            if ((length - i) % 4 == 0 && length - i != length) {
                thousandStr.insert(0, ',');
            }
            i--;
        }
        return thousandStr.toString();
    }

}
